//
//  MainTabBarViewController.m
//  ToolFramework
//
//  Created by apple on 2017/12/25.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "BaseTabBarViewController.h"



//用到的storyboard模块
//@"1",@"2",@"3",@"4",@"5"
#define kStoryNameArray @[@"1",@"2",@"3",@"5"]

//标签栏文字
#define kTitleNameArray @[@"11选5",@"号码历史",@"吐槽小区",@"我的"]
//标签栏背景色
#define kTabBarColor kRGBColor(223, 224, 225)
//标签栏文字-未点击颜色
//    [UIColor grayColor]
//    [UIColor blackColor]
//    kRGBColor(199, 199, 199)
#define kItemTitleColorNormal [UIColor blackColor]
//标签栏文字-点击颜色
#define kItemTitleColorSelected  kRGBColor(0, 147, 255)



//@"1",@"2",@"3",@"4",@"5"
#define kItemImageNameArray @[@"1",@"2",@"3",@"4",@"5"]
//拼接storyboard名称
#define kStoryNameString(x) [NSString stringWithFormat:@"Storyboard_%@",x]

//拼接标签栏未点击图片名称
#define kImgNormalNameString(x) [NSString stringWithFormat:@"标签栏_%@",x]

//拼接标签栏点击图片名称
#define kImgSelectedNameString(x) [NSString stringWithFormat:@"标签栏_%@点击",x]


@interface BaseTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation BaseTabBarViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [Home523Manager shared].lotteryName = @"11选5";
    [self setTabbarItemForProject];
    
}


#pragma mark - 自定义TabBarController样式
-(void)setTabbarItemForProject{
   
    NSMutableArray *vcs=[NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i<[kStoryNameArray count]; i++) {
        UINavigationController *navigationController=[[UINavigationController alloc]init];
        
        navigationController.hidesBottomBarWhenPushed = YES;
        navigationController.navigationBar.barStyle = UIBarStyleBlack;
        navigationController.navigationBar.barTintColor = kNavigationBarColor;
        navigationController.navigationBar.tintColor = kNavigationBarTitleColor;
        navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kNavigationBarTitleColor};
        navigationController.navigationBar.translucent = NO;
   
        NSString* imgNormalName = kImgNormalNameString(kItemImageNameArray[i]);
        NSString* imgSelectedName = kImgSelectedNameString(kItemImageNameArray[i]);
        
        UIImage *imgNormal = [[UIImage imageNamed:imgNormalName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *imgSelected = [[UIImage imageNamed:imgSelectedName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        UITabBarItem* tbItem = [[UITabBarItem alloc] initWithTitle:kTitleNameArray[i] image:imgNormal selectedImage:imgSelected];
        
        //用于中间位移了的按钮
        //        if (i==2) {
        ////            UIEdgeInsets indsets = UIEdgeInsetsMake(-19, 0, 19, 0);
        //            UIEdgeInsets indsets = UIEdgeInsetsMake(-19, 0, 0, 0);
        //            tbItem.imageInsets = indsets;
        //        }
        
        [tbItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont systemFontOfSize:12], NSFontAttributeName,kItemTitleColorNormal,NSForegroundColorAttributeName,
                                        nil] forState:UIControlStateNormal];//设置未选中字体颜色
        [tbItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont boldSystemFontOfSize:14], NSFontAttributeName,kItemTitleColorSelected,NSForegroundColorAttributeName,
                                        nil] forState:UIControlStateSelected];//设置选择时字体颜色
        
        navigationController.tabBarItem = tbItem;
        [vcs addObject:navigationController];
    }
    
    self.tabBar.barTintColor = kTabBarColor;
 
    //交换
    for (NSInteger i = 0; i < vcs.count; i++) {
        UINavigationController *nav = [vcs objectAtIndex:i];
        NSArray *native = @[[[self getModuleVC] objectAtIndex:i]];
        [nav setViewControllers:native];
    }
    [self setViewControllers:vcs animated:NO];
    self.delegate = self;
    self.selectedIndex = 0;
    
    
}
-(NSArray *)getModuleVC{
    NSMutableArray *viewControlls = [NSMutableArray new];
    
        
    for (int i = 0; i < kStoryNameArray.count; i++) {
        if (i==4) {
            
        }
        if (i==0) {
            
            NSString* storyboardName = @"Home0523";
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
            [viewControlls addObject:[storyBoard instantiateViewControllerWithIdentifier:@"HomeFor523ViewController"]];
        }else if (i==1) {
            
            //设置名称
//            NSString* storyboardName = @"Announcement";
//            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
//            [viewControlls addObject:[storyBoard instantiateViewControllerWithIdentifier:@"AnnouncementViewController"]];

            UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"WinNumHistory" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"WinNumHistoryViewController"];
            [viewControlls addObject:acViewCon];
        }else if (i==2){
            
//            //设置名称
//            NSString* storyboardName = @"TrendForLot";
//            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
//            [viewControlls addObject:[storyBoard instantiateViewControllerWithIdentifier:@"TrendViewController"]];
//            NSString* storyboardName = @"Home0523";
//            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
            [viewControlls addObject:[[UIStoryboard storyboardWithName:@"BBSStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"BBSController"]];
        }else if (i==3){
            //设置名称
            NSString* storyboardName = @"Storyboard_5";
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
            [viewControlls addObject:[storyBoard instantiateViewControllerWithIdentifier:@"ModuleVC5"]];
        }
        else {
            //设置名称
            NSString* storyboardName = kStoryNameString([kStoryNameArray objectAtIndex:i]);
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
            [viewControlls addObject:[storyBoard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"ModuleVC%d",i+1]]];
        }
        
        

        
        
    }
        
       
        
    
    return [viewControlls copy];  ;
}
#pragma mark - 打开登录界面
-(void)openLoginAction{
    
    LoginController *loginVC = [[UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil]instantiateViewControllerWithIdentifier:@"LoginController"];
    loginVC.hidesBottomBarWhenPushed = YES;
    [self.selectedViewController pushViewController:loginVC animated:YES];
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if (!([kUserID length]>0)) {
        if (self.selectedIndex == 0){
            [Home523Manager shared].lotteryName = @"11选5";

            return;
        }
        if (self.selectedIndex==1) {
            [Home523Manager shared].lotteryName = @"夺金11选5";
            
//            self.selectedIndex = 0;
                        return;
        }
        if (self.selectedIndex==2) {
            [Home523Manager shared].lotteryName = @"江苏11选5";
//            self.selectedIndex = 0;
                        return;
        }
        if (self.selectedIndex==3) {
            self.selectedIndex = 0;
//            return;
        }
        if (self.selectedIndex==4) {
            self.selectedIndex = 0;
            
        }
        
        [self openLoginAction];
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
